#include <list>
#include <iostream>

using std::cout;
using std::endl;
using std::list;

template <class Container>
void display(const Container & c)
{//形参使用了const关键字，则迭代器也必须要是const的
    typename Container::const_iterator it = c.begin();
    while(it != c.end()) {
        cout << *it << " ";
        ++it;
    }
    cout << endl;
}

void test0()
{
    list<int> numbers1{3, 5, 1, 2, 4};
    list<int> numbers2{13, 15, 11, 12, 14};
    cout << "numbers1:";
    display(numbers1);
    cout << "numbers2:";
    display(numbers2);

    //merge之前的元素是无序的，merge之后也无序
    numbers1.merge(numbers2);
    cout << "合并之后:" << endl;
    cout << "numbers1:";
    display(numbers1);
    cout << "numbers2:";
    display(numbers2);
}

void test1()
{
    list<int> numbers1{3, 5, 1, 2, 24};
    list<int> numbers2{13, 15, 11, 12, 14};
    //双向链表自带一个排序函数
    //默认情况下按升序排序
    numbers1.sort();
    numbers2.sort();
    cout << "排序之后:" << endl;
    cout << "numbers1:";
    display(numbers1);
    cout << "numbers2:";
    display(numbers2);

    //merge之前的元素是有序的，merge之后依然有序
    numbers1.merge(numbers2);
    cout << "合并之后:" << endl;
    cout << "numbers1:";
    display(numbers1);
    cout << "numbers2:";
    display(numbers2);
}

//普通函数的实现
bool compare(int x, int y)
{
    return x > y;
}

struct Comparator
{
    bool operator()(int x, int y)
    {
        return x > y;
    }
};

void test2()
{
    list<int> numbers1{3, 5, 1, 2, 24};
    /* numbers1.sort(compare); */
    /* numbers1.sort(Comparator()); */
    cout << "numbers1:";
    display(numbers1);

    //标准库提供了针对于任意类型的比较器大于符号
    std::greater<int> gt;
    cout << gt(1, 2) << endl;
    cout << gt(2, 1) << endl;

    numbers1.sort(gt);
    cout << "numbers1:";
    display(numbers1);
}

bool compare2(int n)
{
    return n > 3;
}


void test3()
{
    list<int> numbers1{3, 5, 3, 3, 1, 2, 3, 3,6, 7, 3, 24};
    numbers1.sort();
    cout << "numbers1:";
    display(numbers1);
    numbers1.unique();
    cout << "调用unique函数之后:" << endl;
    cout << "numbers1:";
    display(numbers1);

    cout << "\n调用reverse函数之后:" << endl;
    numbers1.reverse();
    cout << "numbers1:";
    display(numbers1);

    numbers1.remove(24);
    cout << "numbers1:";
    display(numbers1);
    numbers1.remove_if(compare2);
    cout << "numbers1:";
    display(numbers1);
}

void test4()
{
    list<int> numbers1{1, 3, 5, 7, 9};
    list<int> numbers2{11, 12, 13};

    cout << "numbers1:";
    display(numbers1);
    cout << "numbers2:";
    display(numbers2);

    auto it2 = numbers2.begin();
    ++it2;//指向12
    auto it1 = numbers1.begin();
    ++it1;//指向3
    cout << ">> 执行splice操作:" << endl;
    numbers1.splice(it1, numbers2, it2);
    cout << "numbers1:";
    display(numbers1);
    cout << "numbers2:";
    display(numbers2);
    cout << "*it1:" << *it1 << endl;
    cout << "*it2:" << *it2 << endl;

    //将整个numbers2全部移动到numbers1中
    cout << "\n 第二次执行splice操作:" <<endl;
    numbers1.splice(numbers1.begin(), numbers2);
    cout << "numbers1:";
    display(numbers1);
    cout << "numbers2:";
    display(numbers2);

    //将一条链表中的某个元素移动到特定的位置
    it1 = numbers1.end();
    --it1;
    --it1;
    cout << "\n*it1:" << *it1 << endl;
    numbers1.splice(numbers1.begin(), numbers1, it1);
    cout << "numbers1:";
    display(numbers1);
    it1 = numbers1.end();
    --it1;
    --it1;
    --it1;
    numbers1.splice(numbers1.begin(), numbers1, it1, numbers1.end());
    cout << "numbers1:";
    display(numbers1);
}

int main()
{
    /* test0(); */
    /* test1(); */
   /* test2(); */
   /* test3(); */
   test4();
    return 0;
}

